package com.simple.datax.utils;

import com.simple.datax.SimpleFailMessage;
import com.simple.datax.SimpleMessage;

/**
 * @Title: MessageUtils.java
 * @Package com.simple.datax.utils
 * @Description: TODO(添加描述)
 * @author Paladin Chen
 * @date 2012-12-27 下午6:02:33
 * @version V1.0
 */
public class MessageUtils {

	public static SimpleFailMessage castFailMessage(String errorInfo,
			short errorType, short errorLevel, SimpleMessage src) {
		SimpleFailMessage sfm = new SimpleFailMessage();
		sfm.setErrorInfo(errorInfo);
		sfm.setErrorLevel(errorLevel);
		sfm.setErrorType(errorType);
		sfm.setMsgFormate(src.getMsgFormat());
		sfm.setMsgType(src.getMsgType());
		sfm.setRecvRawMsgid(src.getRecvRawMsgid());
		sfm.setMsgReceiver(src.getReceiver());
		sfm.setMessageid(src.getMessageid());
		sfm.setSendRawMsgid(src.getSentMessageid());
		sfm.setMsgSender(src.getSender());

		return sfm;
	}
	
	public static SimpleFailMessage castFailMessage(String errorInfo,
			short errorType, short errorLevel, String msgFormat, String msgType,String recvRawMsgid,
			String msgReceiver,String msgSender, String messageid, String sentRawMsgid) {
		SimpleFailMessage sfm = new SimpleFailMessage();
		sfm.setErrorInfo(errorInfo);
		sfm.setErrorLevel(errorLevel);
		sfm.setErrorType(errorType);
		sfm.setMsgFormate(msgFormat);
		sfm.setMsgType(msgType);
		sfm.setRecvRawMsgid(recvRawMsgid);
		sfm.setMsgReceiver(msgReceiver);
		sfm.setMessageid(messageid);
		sfm.setSendRawMsgid(sentRawMsgid);
		sfm.setMsgSender(msgSender);

		return sfm;
	}

	private static String LOCKER_ID = "";

	/**
	 * 指定前缀+17位时间戳生成唯一报文编号
	 * 
	 * @return
	 */
	public synchronized static String BuildMessageId(String prefix) {
		String messageId = prefix + "-" + DateUtils.getTimeStamp("yyyyMMddHHmmssSSS");
		if (messageId.equals(LOCKER_ID) == false) {
			LOCKER_ID = messageId;
			return messageId;
		} else {
			return BuildMessageId(prefix);
		}

	}
}
